﻿using System;
using System.Globalization;
using Loki.Utils;

namespace Luna.Connectors.ZohoCRM
{
    public sealed class ZohoSearchCondition : LokiObject
    {
        public ZohoSearchCondition(string P_Label, ConditionOperator P_Operator, object P_Value)
        {
            if (P_Label == null)
            {
                throw BuildErrorFormat<ArgumentNullException>(Resources.Errors.ASSERT_LABEL_REQUIRED);
            }

            if (P_Value == null)
            {
                throw BuildErrorFormat<ArgumentNullException>(Resources.Errors.ASSERT_VALUE_REQUIRED);
            }

            Label = P_Label;
            Operator = P_Operator;
            Value = P_Value;
        }

        public string Label { get; private set; }

        public ConditionOperator Operator { get; private set; }

        public object Value { get; private set; }

        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, SerializerParams.CONDITION_FORMAT, Label, OperatorString(Operator), Value);
        }

        private static string ValueString(ConditionOperator P_Operator, object P_Value)
        {
            string L_StringValue = P_Value as string;
            if (L_StringValue == null)
            {
                L_StringValue = Convert.ToString(P_Value);
            }

            switch (P_Operator)
            {
                case ConditionOperator.Contains:
                case ConditionOperator.NotContains:
                    return string.Format(SerializerParams.CONDITION_VALUE_CONTAINS_PATTERN, L_StringValue);

                case ConditionOperator.StartsWith:
                    return string.Format(SerializerParams.CONDITION_VALUE_START_PATTERN, L_StringValue);

                case ConditionOperator.EndsWith:
                    return string.Format(SerializerParams.CONDITION_VALUE_END_PATTERN, L_StringValue);

                default:
                    return L_StringValue;
            }
        }

        private string OperatorString(ConditionOperator P_Operator)
        {
            switch (P_Operator)
            {
                case ConditionOperator.Is:
                    return SerializerParams.CONDITION_OPERATOR_IS;

                case ConditionOperator.IsNot:
                    return SerializerParams.CONDITION_OPERATOR_ISNOT;

                case ConditionOperator.Contains:
                    return SerializerParams.CONDITION_OPERATOR_CONTAINS;

                case ConditionOperator.NotContains:
                    return SerializerParams.CONDITION_OPERATOR_NOTCONTAINS;

                case ConditionOperator.StartsWith:
                    return SerializerParams.CONDITION_OPERATOR_STARTWITH;

                case ConditionOperator.EndsWith:
                    return SerializerParams.CONDITION_OPERATOR_ENDSWITH;

                case ConditionOperator.LesserThan:
                    return SerializerParams.CONDITION_OPERATOR_LESSER_THAN;

                case ConditionOperator.GreaterThan:
                    return SerializerParams.CONDITION_OPERATOR_GREATER_THAN;

                case ConditionOperator.LesserOrEqualsThan:
                    return SerializerParams.CONDITION_OPERATOR_LESSER_THAN_OR_EQUALS;

                case ConditionOperator.GreaterOrEqualThan:
                    return SerializerParams.CONDITION_OPERATOR_GREATER_THAN_OR_EQUALS;

                default:
                    throw BuildErrorFormat<NotSupportedException>(Resources.Errors.INVALID_FORMAT_OPERATOR, P_Operator);
            }
        }
    }
}